package com.api.gbhg.liuwenbo.test;

import com.kingbase8.util.LOGGER;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.*;

@Path("gb/exceltest")
public class test {

    @GET
    @Path("/excel")
    @Produces(MediaType.APPLICATION_OCTET_STREAM)
    public static HttpServletResponse excel(@Context HttpServletResponse response, @Context HttpServletRequest request){
        // //excel文件名
//创建HSSFWorkbook对象(excel的文档对象)

        HSSFWorkbook workbook = new HSSFWorkbook();
        //创建sheet对象（excel的表单）
        HSSFSheet sheet=workbook.createSheet("sheet1");

            //创建第一行，这里即是表头。行的最小值是0，代表每一行，上限没研究过，可参考官方的文档
            Row row = sheet.createRow(1);
            //在这一行创建单元格，并且将这个单元格的内容设为“账号”，下面同理。
            //列的最小值标识也是0
            row.createCell(0).setCellValue("账号");
            row.createCell(1).setCellValue("用户名");
            row.createCell(2).setCellValue("日期");
            row.createCell(3).setCellValue("是否完成");


        //输出Excel文件
        response.setHeader("Content-Disposition","attachment;filename="+"a" +".xls");
        response.setHeader("Cache-Control","no-cache");
        ServletOutputStream out = null;
        try {
            out = response.getOutputStream();
            workbook.write(out);
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    return response;
    }
}
